# NetTools 项目构建脚本
.PHONY: all server client clean build test install help

# 变量定义
APP_NAME = nettools
VERSION = 1.0.0
BUILD_TIME = $(shell date +%Y-%m-%d\ %H:%M:%S)
GO_VERSION = $(shell go version)

# 构建目录
BUILD_DIR = build
SERVER_BINARY = $(BUILD_DIR)/nettools-server
CLIENT_BINARY = $(BUILD_DIR)/nettools-client

# Go构建标志
LDFLAGS = -ldflags "-X main.AppVersion=$(VERSION) -X 'main.BuildTime=$(BUILD_TIME)'"

# 默认目标
all: clean build

# 显示帮助信息
help:
	@echo "NetTools 构建脚本"
	@echo ""
	@echo "可用命令:"
	@echo "  make all      - 清理并构建所有程序"
	@echo "  make build    - 构建服务端和客户端"
	@echo "  make server   - 只构建服务端"
	@echo "  make client   - 只构建客户端"
	@echo "  make test     - 运行测试"
	@echo "  make clean    - 清理构建文件"
	@echo "  make install  - 安装到系统目录"
	@echo "  make help     - 显示此帮助信息"
	@echo ""
	@echo "版本: $(VERSION)"
	@echo "Go版本: $(GO_VERSION)"

# 构建所有程序
build: server client

# 构建服务端
server:
	@echo "正在构建服务端..."
	@mkdir -p $(BUILD_DIR)
	@cd server && go build $(LDFLAGS) -o ../$(SERVER_BINARY) .
	@echo "服务端构建完成: $(SERVER_BINARY)"

# 构建客户端
client:
	@echo "正在构建客户端..."
	@mkdir -p $(BUILD_DIR)
	@cd client && go build $(LDFLAGS) -o ../$(CLIENT_BINARY) .
	@echo "客户端构建完成: $(CLIENT_BINARY)"

# 跨平台构建
build-all: clean
	@echo "正在进行跨平台构建..."
	@mkdir -p $(BUILD_DIR)
	
	# Windows 64位
	@echo "构建 Windows 64位版本..."
	@cd server && GOOS=windows GOARCH=amd64 go build $(LDFLAGS) -o ../$(BUILD_DIR)/nettools-server-windows-amd64.exe .
	@cd client && GOOS=windows GOARCH=amd64 go build $(LDFLAGS) -o ../$(BUILD_DIR)/nettools-client-windows-amd64.exe .
	
	# Linux 64位
	@echo "构建 Linux 64位版本..."
	@cd server && GOOS=linux GOARCH=amd64 go build $(LDFLAGS) -o ../$(BUILD_DIR)/nettools-server-linux-amd64 .
	@cd client && GOOS=linux GOARCH=amd64 go build $(LDFLAGS) -o ../$(BUILD_DIR)/nettools-client-linux-amd64 .
	
	# macOS 64位
	@echo "构建 macOS 64位版本..."
	@cd server && GOOS=darwin GOARCH=amd64 go build $(LDFLAGS) -o ../$(BUILD_DIR)/nettools-server-darwin-amd64 .
	@cd client && GOOS=darwin GOARCH=amd64 go build $(LDFLAGS) -o ../$(BUILD_DIR)/nettools-client-darwin-amd64 .
	
	# ARM64 (Apple Silicon)
	@echo "构建 macOS ARM64版本..."
	@cd server && GOOS=darwin GOARCH=arm64 go build $(LDFLAGS) -o ../$(BUILD_DIR)/nettools-server-darwin-arm64 .
	@cd client && GOOS=darwin GOARCH=arm64 go build $(LDFLAGS) -o ../$(BUILD_DIR)/nettools-client-darwin-arm64 .
	
	@echo "跨平台构建完成！"
	@ls -la $(BUILD_DIR)/

# 运行测试
test:
	@echo "运行测试..."
	@go test -v ./...

# 清理构建文件
clean:
	@echo "清理构建文件..."
	@rm -rf $(BUILD_DIR)
	@echo "清理完成"

# 安装到系统目录 (需要管理员权限)
install: build
	@echo "安装到系统目录..."
	@sudo cp $(SERVER_BINARY) /usr/local/bin/nettools-server
	@sudo cp $(CLIENT_BINARY) /usr/local/bin/nettools-client
	@sudo chmod +x /usr/local/bin/nettools-server
	@sudo chmod +x /usr/local/bin/nettools-client
	@echo "安装完成"

# 创建发布包
release: build-all
	@echo "创建发布包..."
	@mkdir -p $(BUILD_DIR)/release
	
	# Windows 发布包
	@mkdir -p $(BUILD_DIR)/release/nettools-$(VERSION)-windows-amd64
	@cp $(BUILD_DIR)/nettools-server-windows-amd64.exe $(BUILD_DIR)/release/nettools-$(VERSION)-windows-amd64/
	@cp $(BUILD_DIR)/nettools-client-windows-amd64.exe $(BUILD_DIR)/release/nettools-$(VERSION)-windows-amd64/
	@cp -r configs $(BUILD_DIR)/release/nettools-$(VERSION)-windows-amd64/
	@cp -r doc $(BUILD_DIR)/release/nettools-$(VERSION)-windows-amd64/
	@cd $(BUILD_DIR)/release && zip -r nettools-$(VERSION)-windows-amd64.zip nettools-$(VERSION)-windows-amd64/
	
	# Linux 发布包
	@mkdir -p $(BUILD_DIR)/release/nettools-$(VERSION)-linux-amd64
	@cp $(BUILD_DIR)/nettools-server-linux-amd64 $(BUILD_DIR)/release/nettools-$(VERSION)-linux-amd64/
	@cp $(BUILD_DIR)/nettools-client-linux-amd64 $(BUILD_DIR)/release/nettools-$(VERSION)-linux-amd64/
	@cp -r configs $(BUILD_DIR)/release/nettools-$(VERSION)-linux-amd64/
	@cp -r doc $(BUILD_DIR)/release/nettools-$(VERSION)-linux-amd64/
	@cd $(BUILD_DIR)/release && tar -czf nettools-$(VERSION)-linux-amd64.tar.gz nettools-$(VERSION)-linux-amd64/
	
	# macOS 发布包
	@mkdir -p $(BUILD_DIR)/release/nettools-$(VERSION)-darwin-amd64
	@cp $(BUILD_DIR)/nettools-server-darwin-amd64 $(BUILD_DIR)/release/nettools-$(VERSION)-darwin-amd64/
	@cp $(BUILD_DIR)/nettools-client-darwin-amd64 $(BUILD_DIR)/release/nettools-$(VERSION)-darwin-amd64/
	@cp -r configs $(BUILD_DIR)/release/nettools-$(VERSION)-darwin-amd64/
	@cp -r doc $(BUILD_DIR)/release/nettools-$(VERSION)-darwin-amd64/
	@cd $(BUILD_DIR)/release && tar -czf nettools-$(VERSION)-darwin-amd64.tar.gz nettools-$(VERSION)-darwin-amd64/
	
	@echo "发布包创建完成："
	@ls -la $(BUILD_DIR)/release/

# 快速运行 (用于开发测试)
run-server: server
	@echo "启动服务端..."
	@./$(SERVER_BINARY) -config configs/server-config.json

run-client-internal: client
	@echo "启动内网客户端..."
	@./$(CLIENT_BINARY) -config configs/client-internal.json

run-client-external: client
	@echo "启动外部客户端..."
	@./$(CLIENT_BINARY) -config configs/client-external.json

# 初始化项目 (创建必要的目录)
init:
	@echo "初始化项目目录..."
	@mkdir -p logs
	@mkdir -p configs
	@mkdir -p build
	@echo "项目初始化完成" 